package com.hhj.sharding.sphere.demo.controller;

import cn.hutool.core.util.RandomUtil;
import com.hhj.sharding.sphere.demo.base.PageVO;
import com.hhj.sharding.sphere.demo.base.Result;
import com.hhj.sharding.sphere.demo.model.Table2ParamVO;
import com.hhj.sharding.sphere.demo.model.TableParamVO;
import com.hhj.sharding.sphere.demo.model.entity.SingleTable;
import com.hhj.sharding.sphere.demo.model.entity.SingleTable2;
import com.hhj.sharding.sphere.demo.service.ISingleTable2Service;
import com.hhj.sharding.sphere.demo.service.ISingleTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * 单表接口
 *
 * @Description
 * @Author HHJ
 * @Date 2022-02-28 18:28
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/table")
public class SingleTableController {
    @Autowired
    private ISingleTableService singleTableService;
    @Autowired
    private ISingleTable2Service singleTable2Service;

    /**
     * 测试分库分表-单表分页
     */
    @PostMapping("/findPage")
    public Result<PageVO> findPage(@RequestBody TableParamVO req) {
        log.info("测试分库分表1-单表分页");
        return Result.success(singleTableService.findPage(req));
    }

    /**
     * 测试分库分表-新增
     */
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody TableParamVO req) {
        log.info("测试分库分表1-单表新增");
        singleTableService.save(buildOrder(null, "1" + RandomUtil.randomLong(0, 10000000), req.getCode()));
        return Result.success(true);
    }

    /**
     * 测试分库分表 - 修改
     */
    @PostMapping("/updateById")
    public Result<Boolean> updateById(@RequestBody TableParamVO req) {
        log.info("测试分库分表1- 单表修改");
        singleTableService.updateById(buildOrder(req.getId(), req.getTitle(), req.getCode()));
        return Result.success(true);
    }

    /**
     * 测试分库分表- 删除
     */
    @PostMapping("/deleteById")
    public Result<Boolean> deleteById(@RequestBody TableParamVO req) {
        log.info("测试分库分表1- 单表删除");
        singleTableService.removeById(req.getId());
        return Result.success(true);
    }

    /**
     * 测试分库分表-单表分页
     */
    @PostMapping("/findPage2")
    public Result<PageVO> findPage2(@RequestBody Table2ParamVO req) {
        log.info("测试分库分表1-单表分页");
        return Result.success(singleTable2Service.findPage(req));
    }

    /**
     * 测试分库分表-新增
     */
    @PostMapping("/save2")
    public Result<Boolean> save2(@RequestBody Table2ParamVO req) {
        log.info("测试分库分表1-单表新增");
        singleTable2Service.save(buildOrder2(null, "1" + RandomUtil.randomLong(0, 10000000), req.getName(), req.getCode()));
        return Result.success(true);
    }

    /**
     * 测试分库分表 - 修改
     */
    @PostMapping("/updateById2")
    public Result<Boolean> updateById2(@RequestBody Table2ParamVO req) {
        log.info("测试分库分表1- 单表修改");
        singleTable2Service.updateById(buildOrder2(req.getId(), req.getTitle(), req.getName(), req.getCode()));
        return Result.success(true);
    }

    /**
     * 测试分库分表- 删除
     */
    @PostMapping("/deleteById2")
    public Result<Boolean> deleteById2(@RequestBody TableParamVO req) {
        log.info("测试分库分表1- 单表删除");
        singleTable2Service.removeById(req.getId());
        return Result.success(true);
    }

    private SingleTable buildOrder(Long id, String userName, String code) {
        SingleTable entity = new SingleTable();
        entity.setId(id);
        entity.setCode(Objects.nonNull(code) ? code.toString() : "");
        entity.setTitle(Objects.nonNull(userName) ? userName.toString() : "");
        return entity;
    }

    private SingleTable2 buildOrder2(Long id, String userName, String name, String code) {
        SingleTable2 entity = new SingleTable2();
        entity.setId(id);
        entity.setCode(Objects.nonNull(code) ? code.toString() : "");
        entity.setName(Objects.nonNull(name) ? name.toString() : "");
        entity.setTitle(Objects.nonNull(userName) ? userName.toString() : "");
        return entity;
    }

}
